<?php
/**
 * 管理员操作类
 * Created by PhpStorm.
 * User: 王海东
 * Date: 2018/12/26
 * Time: 16:49
 */
namespace app\common\model;

use think\Db;

class OperationLog
{
    private const TABLE_NAME = 'operation_log';

    /**
     * 记录操作日志
     * @author 王海东
     * @date
     * @return void
     * @throws \Exception
     */
    public function setLog()
    {
        $data['action']      = strtolower(URL_PATH);
        $data['action_name'] = $this->getActionDesc($data['action']);

        if (empty($data['action_name']))
            return;

        $data['create_time']  = date("Y-m-d H:i:s");
        $data['admin_id']     = UID;
        $data['operation_ip'] = get_client_ip();

        if (request()->isGet()) {
            $data['params'] = json_encode($_GET,JSON_UNESCAPED_UNICODE);
        } elseif (request()->isPost()) {
            $data['params'] = json_encode($_GET,JSON_UNESCAPED_UNICODE);
        }


        if (request()->isAjax()) {
            $data['request_method'] = 'AJAX';
        } elseif (request()->isPost()) {
            $data['request_method'] = 'POST';
        } elseif (request()->isGet()) {
            $data['request_method'] = 'GET';
        }

        try {
            Db::name(self::TABLE_NAME)
              ->insert($data);
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * 操作描述
     * @author 王海东
     * @date
     * @param string $action    路由地址
     * @return array
     */
    private function getActionDesc(string $action)
    {
        $desc = [
            'adm/admin/add'                 => '管理员管理-》添加管理员',
            'adm/admin/editpwd'             => '管理员管理-》修改密码',
            'adm/admin/delete'              => '管理员管理-》删除管理员',

            'adm/article/addcatalog'        => '文章管理-》添加栏目',
            'adm/article/delcatalog'        => '文章管理-》删除栏目',
            'adm/article/editcatalog'       => '文章管理-》修改栏目',
            'adm/holiday/addarticle'        => '文章管理-》发布文章',
            'adm/holiday/delarticle'        => '文章管理-》删除文章',
            'adm/holiday/editarticle'       => '文章管理-》编辑文章',

            'adm/bankinfo/delete'           => '银行卡管理-》删除银行卡',
            'adm/bankinfo/edit'             => '银行卡管理-》修改银行卡',

            'adm/hierarchy/add'             => '层级管理-》添加层级',
            'adm/hierarchy/edit'            => '层级管理-》修改层级',
            'adm/hierarchy/delete'          => '层级管理-》删除层级',

            'adm/user/updatebalance'        => '用户管理-》修改用户余额',
            'adm/user/editreal'             => '用户管理-》修改实名认证审核',
            'adm/user/frozen'               => '用户管理-》冻结用户',

            'adm/com/setcfg'                => '系统配置-》修改配置信息',
            'adm/msg/sendmsg'               => '消息管理-》发送站内信',
            'adm/capital/editwithdraw'      => '资金管理-》提现审核',
        ];

        try {
            return $desc[ $action ];
        } catch (\Exception $e) {
            return [];
        }
    }

    //查询操作日志
    public function list(int $admin_id)
    {

    }
}